package com.valotas.taglibs.scripting;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.DynamicAttributes;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * This class is the main imlementation of our tag and the only
 * basic thing that does is to put all the given DynamicAttributes
 * to the attr Map
 */
public abstract class InvokeScriptTag extends TagSupport implements DynamicAttributes {
	private static final long serialVersionUID = 361498311198618191L;
	private static final Logger logger = Logger.getLogger(InvokeScriptTag.class.getName());
	
	protected Map<String, Object> attr; //The given dynamic attributes
	protected String name; //The name of the script we have to invoke. the only mandatory non dynamic attribute
	
	
	public InvokeScriptTag() {
		attr = new LinkedHashMap<String, Object>();
	}
	
	/**
	 * The name of the script we have to execute. This is the only
	 * mandatory, non dynamic attribute of our tag library.
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	@Override
	public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
		attr.put(name, value);
	}
	
	@Override
	public int doEndTag() throws JspException {
		if (logger.isLoggable(Level.FINER)) logger.log(Level.FINER, String.format("Script '%s' has been executed", name));

		return super.doEndTag();
	}
}
